{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Solve heat conduction on a square with a uniform area specific heat source"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![cant.png](figures/heat_square.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "using JuAFEM\n",
    "using UnicodePlots"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "grid = generate_grid(Quadrilateral, (20,20))\n",
    "addnodeset!(grid, \"boundary\", x -> abs(x[1]) ≈ 1 ||  abs(x[2]) ≈ 1);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dim = 2\n",
    "ip = Lagrange{dim, RefCube, 1}()\n",
    "qr = QuadratureRule{dim, RefCube}(2)\n",
    "cellvalues = CellScalarValues(qr, ip);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DofHandler\n",
       "  Fields:\n",
       "    T dim: 1\n",
       "  Total dofs: 441\n",
       "  Dofs per cell: 4"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dh = DofHandler(grid)\n",
    "push!(dh, :T, 1) # Add a temperature field\n",
    "close!(dh)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dbc = DirichletBoundaryConditions(dh)\n",
    "add!(dbc, :T, getnodeset(grid, \"boundary\"), (x,t) -> 0.0)\n",
    "close!(dbc)\n",
    "update!(dbc, 0.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[37m                    Sparsity Pattern\n",
       "\u001b[39m\u001b[37m       ┌──────────────────────────────────────────┐\u001b[39m    \n",
       "     \u001b[37m1\u001b[39m\u001b[37m │\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m│\u001b[39m \u001b[31m> 0\u001b[39m\n",
       "      \u001b[37m\u001b[39m\u001b[37m │\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m│\u001b[39m \u001b[34m< 0\u001b[39m\n",
       "      \u001b[37m\u001b[39m\u001b[37m │\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m│\u001b[39m \u001b[37m\u001b[39m   \n",
       "      \u001b[37m\u001b[39m\u001b[37m │\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m│\u001b[39m \u001b[37m\u001b[39m   \n",
       "      \u001b[37m\u001b[39m\u001b[37m │\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣆\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m│\u001b[39m \u001b[37m\u001b[39m   \n",
       "      \u001b[37m\u001b[39m\u001b[37m │\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⢦\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m│\u001b[39m \u001b[37m\u001b[39m   \n",
       "      \u001b[37m\u001b[39m\u001b[37m │\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m│\u001b[39m \u001b[37m\u001b[39m   \n",
       "      \u001b[37m\u001b[39m\u001b[37m │\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m│\u001b[39m \u001b[37m\u001b[39m   \n",
       "      \u001b[37m\u001b[39m\u001b[37m │\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m│\u001b[39m \u001b[37m\u001b[39m   \n",
       "      \u001b[37m\u001b[39m\u001b[37m │\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m│\u001b[39m \u001b[37m\u001b[39m   \n",
       "   \u001b[37m\u001b[39m   \u001b[37m\u001b[39m\u001b[37m │\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣤\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m│\u001b[39m \u001b[37m\u001b[39m   \n",
       "      \u001b[37m\u001b[39m\u001b[37m │\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[31m⠛\u001b[39m\u001b[31m⣦\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m│\u001b[39m \u001b[37m\u001b[39m   \n",
       "      \u001b[37m\u001b[39m\u001b[37m │\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m│\u001b[39m \u001b[37m\u001b[39m   \n",
       "      \u001b[37m\u001b[39m\u001b[37m │\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m│\u001b[39m \u001b[37m\u001b[39m   \n",
       "      \u001b[37m\u001b[39m\u001b[37m │\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m│\u001b[39m \u001b[37m\u001b[39m   \n",
       "      \u001b[37m\u001b[39m\u001b[37m │\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[31m⠳\u001b[39m\u001b[31m⣦\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m│\u001b[39m \u001b[37m\u001b[39m   \n",
       "      \u001b[37m\u001b[39m\u001b[37m │\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[31m⠹\u001b[39m\u001b[31m⣦\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m│\u001b[39m \u001b[37m\u001b[39m   \n",
       "      \u001b[37m\u001b[39m\u001b[37m │\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m│\u001b[39m \u001b[37m\u001b[39m   \n",
       "      \u001b[37m\u001b[39m\u001b[37m │\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m│\u001b[39m \u001b[37m\u001b[39m   \n",
       "      \u001b[37m\u001b[39m\u001b[37m │\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[37m│\u001b[39m \u001b[37m\u001b[39m   \n",
       "   \u001b[37m441\u001b[39m\u001b[37m │\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[37m⠀\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[31m⠻\u001b[39m\u001b[31m⣦\u001b[39m\u001b[37m│\u001b[39m \u001b[37m\u001b[39m   \n",
       "\u001b[37m       └──────────────────────────────────────────┘\u001b[39m    \n",
       "\u001b[37m       1\u001b[39m\u001b[37m                    \u001b[39m\u001b[37m                    441\n",
       "\u001b[39m\u001b[37m                        nz = 3721\n",
       "\u001b[39m"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "K = create_sparsity_pattern(dh);\n",
    "fill!(K.nzval, 1.0);\n",
    "spy(K)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "function doassemble{dim}(cellvalues::CellScalarValues{dim}, K::SparseMatrixCSC, dh::DofHandler)\n",
    "    b = 1.0\n",
    "    f = zeros(ndofs(dh))\n",
    "    assembler = start_assemble(K, f)\n",
    "    \n",
    "    n_basefuncs = getnbasefunctions(cellvalues)\n",
    "    global_dofs = zeros(Int, ndofs_per_cell(dh))\n",
    "\n",
    "    fe = zeros(n_basefuncs) # Local force vector\n",
    "    Ke = zeros(n_basefuncs, n_basefuncs) # Local stiffness mastrix\n",
    "\n",
    "    @inbounds for (cellcount, cell) in enumerate(CellIterator(dh))\n",
    "        fill!(Ke, 0)\n",
    "        fill!(fe, 0)\n",
    "        \n",
    "        reinit!(cellvalues, cell)\n",
    "        for q_point in 1:getnquadpoints(cellvalues)\n",
    "            dΩ = getdetJdV(cellvalues, q_point)\n",
    "            for i in 1:n_basefuncs\n",
    "                δT = shape_value(cellvalues, q_point, i)\n",
    "                ∇δT = shape_gradient(cellvalues, q_point, i)\n",
    "                fe[i] += (δT * b) * dΩ\n",
    "                for j in 1:n_basefuncs\n",
    "                    ∇T = shape_gradient(cellvalues, q_point, j)\n",
    "                    Ke[i, j] += (∇δT ⋅ ∇T) * dΩ\n",
    "                end\n",
    "            end\n",
    "        end\n",
    "        \n",
    "        celldofs!(global_dofs, cell)\n",
    "        assemble!(assembler, global_dofs, fe, Ke)\n",
    "    end\n",
    "    return K, f\n",
    "end;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "K, f = doassemble(cellvalues, K, dh);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "apply!(K, f, dbc)\n",
    "T = K \\ f;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "vtkfile = vtk_grid(\"heat\", dh)\n",
    "vtk_point_data(vtkfile, dh, T)\n",
    "vtk_save(vtkfile);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Heat square successful\n"
     ]
    }
   ],
   "source": [
    "Base.Test.@test maximum(T) ≈ 0.29526786377073544\n",
    "println(\"Heat square successful\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "julia-0.6",
   "display_name": "Julia 0.6.0",
   "language": "julia"
  },
  "language_info": {
   "mimetype": "application/julia",
   "file_extension": ".jl",
   "version": "0.6.0",
   "name": "julia"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
